<?php
// +----------------------------------------------------------------------
// | ZHANGKUI [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.rndchina.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: zk_admin@163.com
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\ApiProjectModel;
use app\admin\model\ApiprojectresumeModel;
use app\admin\model\UserModel;
use cmf\controller\AdminBaseController;
use FontLib\Table\Type\name;
use think\Db;

/**
 * Func 基础模型类
 *
 * Class BaseController
 *
 * @package app\admin\controller
 */
class BaseController extends AdminBaseController
{
    /**
     * 分页id
     * @var int
     */
    protected $page = 1;

    /**
     * 分页条数
     * @var int
     */
    protected $limit = 10;
    /**
     * 搜索类型
     * @var string
     */
    protected $searchType = '';

    /**
     * 关键词
     * @var string
     */
    protected $keyword = '';
    /**
     * 开始时间
     * @var string
     */
    protected $start_time = '';

    /**
     * 状态
     * @var int
     */
    protected $status = '';

    /**
     * 结束时间
     * @var string
     */
    protected $end_time = '';

    /**
     * 游玩日期
     * @var string
     */
    protected $play_date = '';

    protected function initialize()
    {
        $param = $this->request->param();
        $this->page         = isset($param['page']) ? intval($param['page']) : 1;
        $this->limit        = isset($param['limit']) ? intval($param['limit']) : 10;
        $this->searchType   = isset($param['searchType']) ? $param['searchType'] : '';
        $this->keyword      = isset($param['keyword']) ? trim(strip_tags($param['keyword'])) : '';
        $this->start_time   = isset($param['start_time']) ? $param['start_time'] : '';
        $this->end_time     = isset($param['end_time']) ? $param['end_time'] : '';
        $this->status       = isset($param['status']) ? $param['status'] : '';
        $this->play_date    = isset($param['play_date']) ? $param['play_date'] : '';
        $this->assign('page', $this->page);
        $this->assign('limit', $this->limit);
        $this->assign('searchType', $this->searchType);
        $this->assign('keyword', $this->keyword);
        $this->assign('start_time', $this->start_time);
        $this->assign('end_time', $this->end_time);
        $this->assign('status', $this->status);
        $this->assign('play_date', $this->play_date);
    }

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * @var array 证件类型
     */
    protected static $wordTypeArr = [
        1 => '身份证',
        2 => '护照',
        3 => '城市名',
    ];

    /**
     * @var array 证件类型
     */
    protected static $idTypeArr = [
        1 => '身份证',
        2 => '护照',
    ];

    /**
     * @var array 性别
     */
    protected static $resumeSexArr = [
        '男' => '男',
        '女' => '女',
    ];

    /**
     * @var array 状态
     */
    protected static $wordStatusArr = [
        1 => '通过',
        -1 => '关闭',
    ];

    /**
     * @var array 状态
     */
    protected static $spotStatusArr = [
        1 => '通过',
        2 => '拒绝',
        3 => '待审核',
        -1 => '关闭',
    ];

    /**
     * @var array 状态
     */
    protected static $companyStatusArr = [
        1 => '通过',
        2 => '拒绝',
        3 => '待审核',
        -1 => '关闭',
    ];

    /**
     * @var array 状态
     */
    protected static $supplierStatusArr = [
        1 => '通过',
        2 => '拒绝',
        3 => '待审核',
        -1 => '关闭',
    ];

    /**
     * @var array 状态
     */
    protected static $resumeStatusArr = [
        1 => '通过',
        2 => '拒绝',
        3 => '待审核',
        -1 => '关闭',
    ];

    /**
     * @var array 状态
     */
    protected static $projectStatusArr = [
        1 => '通过',
        2 => '拒绝',
        3 => '待审核',
        -1 => '关闭',
    ];

    /**
     * Func 获取POST参数
     *
     * @param $fieldArr 字段
     *
     * return String|Array
     */
    protected function getPostParamInfo($fieldArr = [])
    {
        $data = [];

        if (!$fieldArr) return $data;

        foreach ($_POST as $key => $val)
        {
            if (in_array($key, $fieldArr))
            {
                $data[$key] = trim($val);
            }
        }
        return (array)$data;

    }

    /**
     * func 通过角色获取用户id
     * return array
     */
    public function getUserIdArr(array $roleIdArr)
    {
        return (array)Db::name('role_user')
            ->whereIn('role_id', $roleIdArr)
            ->column('user_id');
    }

    /**
     * func 手机号查询
     * @return array
     */
    public function getMobileQuery(array $params = [])
    {
        try {
            $secretId = 'fGd6lHURQDlUYpIi';
            $secretKey = 'hCPcKgTV30Nh6mKSKcO0R2uWDBeCHdXj';
            $datetime = gmdate('D, d M Y H:i:s T');
            $signStr = sprintf("x-date: %s", $datetime);
            $sign = base64_encode(hash_hmac('sha1', $signStr, $secretKey, true));
            $auth = sprintf('{"id": "%s", "x-date": "%s" , "signature": "%s"}', $secretId, $datetime, $sign);
            $method = 'GET';
            $headers = [
                'Authorization' => $auth,
            ];
            $queryParams = [
                'phone' => $params['mobile'],
            ];
            $bodyParams = [];
            $sendData = http_build_query($bodyParams);
            $url = 'https://ap-beijing.cloudmarket-apigw.com/service-nh3oernv/phone/query';
            if (count($queryParams) > 0) {
                $url .= '?' . http_build_query($queryParams);
            }
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_TIMEOUT, 60);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);

            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);


            if (in_array($method, array('POST', 'PUT', 'PATCH'), true)) {
                $headers['Content-Type'] = 'application/x-www-form-urlencoded';
                curl_setopt($ch, CURLOPT_POSTFIELDS, $sendData);
            }
            curl_setopt($ch, CURLOPT_HTTPHEADER, array_map(function ($v, $k) {
                return $k . ': ' . $v;
            }, array_values($headers), array_keys($headers)));

            $data = curl_exec($ch);
            if (curl_errno($ch)) {
                return ['status' => false, 'message' => curl_error($ch)];
            }
            curl_close($ch);
            $result = json_decode($data,true);
            if($result['code'] != '0000')
            {
                return ['status' => false, 'message' => $result['data']];
            }
            return ['status' => true, 'message' => 'ok','data'=>$result['appdata']];
        }catch (\Exception $e){
            return ['status' => false, 'message' => $e->getMessage()];
        }
    }

}